package com.shanghai.shanghaielectric.UI.KeyBoard;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import shanghaielectric_lib.Utils.MySystemUtil;


/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class MKeyBoard_NumberInput extends DialogFragment {
    public static final int CHANGEUI = 6262;
    public static final int PSD=663;
    public static final int NUM=664;
    public boolean isMinus = false;

    public int mType = 0;

    @BindView(R.id.et_errorAlert_numberKeyBoard)
    TextView etErrorAlert;
    private StringBuffer buf;
    public Context mContext;
    @BindView(R.id.et_input_numberKeyBoard)
    EditText etInput;

    @BindView(R.id.tv_title_numberKeyBoard)
    TextView title;

    @BindView(R.id.setting_center_key_one_numberKeyBoard)
    TextView settingCenterKeyOne;
    @BindView(R.id.setting_center_key_two_numberKeyBoard)
    TextView settingCenterKeyTwo;
    @BindView(R.id.setting_center_key_three_numberKeyBoard)
    TextView settingCenterKeyThree;
    @BindView(R.id.setting_center_key_delete_numberKeyBoard)
    TextView settingCenterKeyDelete;
    @BindView(R.id.setting_center_key_four_numberKeyBoard)
    TextView settingCenterKeyFour;
    @BindView(R.id.setting_center_key_five_numberKeyBoard)
    TextView settingCenterKeyFive;
    @BindView(R.id.setting_center_key_six_numberKeyBoard)
    TextView settingCenterKeySix;
    @BindView(R.id.setting_center_key_clear_numberKeyBoard)
    TextView settingCenterKeyClear;
    @BindView(R.id.setting_center_key_seven_numberKeyBoard)
    TextView settingCenterKeySeven;
    @BindView(R.id.setting_center_key_eight_numberKeyBoard)
    TextView settingCenterKeyEight;
    @BindView(R.id.setting_center_key_nine_numberKeyBoard)
    TextView settingCenterKeyNine;
    @BindView(R.id.setting_center_key_dot_numberKeyBoard)
    TextView settingCenterKeyDot;
    @BindView(R.id.setting_center_key_zero_numberKeyBoard)
    TextView settingCenterKeyZero;
    @BindView(R.id.setting_center_key_sure_numberKeyBoard)
    TextView settingCenterKeySure;
    @BindView(R.id.setting_center_key_minus_numberKeyBoard)
    TextView settingCenterKeyMinus;

    public static interface MkeyBoardListener_numberKeyBoard {
        void onClink(boolean isCorrect, String value);
    }

    public void setMkeyBoardListener(MkeyBoardListener_numberKeyBoard keyBoardListener) {
        mkeyBoardListener = keyBoardListener;
    }

    public static MkeyBoardListener_numberKeyBoard mkeyBoardListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = inflater.inflate(R.layout.m_keyboard_passwards, container);
        ButterKnife.bind(this, view);


//        Toast.makeText(mContext, this.getTag().toString(), Toast.LENGTH_SHORT).show();
        initView();
        initData();
        return view;
    }

    private void initView() {
        mContext = getContext().getApplicationContext();
        buf = new StringBuffer();
        etInput.setInputType(InputType.TYPE_NULL);

    }

    private void initData() {

    }

    @OnClick({R.id.et_input_numberKeyBoard, R.id.setting_center_key_one_numberKeyBoard, R.id.setting_center_key_two_numberKeyBoard, R.id.setting_center_key_three_numberKeyBoard, R.id.setting_center_key_delete_numberKeyBoard, R.id.setting_center_key_four_numberKeyBoard, R.id.setting_center_key_five_numberKeyBoard, R.id.setting_center_key_six_numberKeyBoard, R.id.setting_center_key_clear_numberKeyBoard, R.id.setting_center_key_seven_numberKeyBoard, R.id.setting_center_key_eight_numberKeyBoard, R.id.setting_center_key_nine_numberKeyBoard, R.id.setting_center_key_dot_numberKeyBoard, R.id.setting_center_key_zero_numberKeyBoard, R.id.setting_center_key_sure_numberKeyBoard, R.id.setting_center_key_minus_numberKeyBoard})
    public void onViewClicked(View view) {
        Message message = new Message();

        if (view.getId() == R.id.setting_center_key_one_numberKeyBoard) {
            buf.append(1);
//            Toast.makeText(mContext, "1", Toast.LENGTH_SHORT).show();
        } else if (view.getId() == R.id.setting_center_key_two_numberKeyBoard) {
            buf.append(2);
//            Toast.makeText(mContext, "2", Toast.LENGTH_SHORT).show();
        } else if (view.getId() == R.id.setting_center_key_three_numberKeyBoard) {
            buf.append(3);
        } else if (view.getId() == R.id.setting_center_key_four_numberKeyBoard) {
            buf.append(4);
        } else if (view.getId() == R.id.setting_center_key_five_numberKeyBoard) {
            buf.append(5);
        } else if (view.getId() == R.id.setting_center_key_six_numberKeyBoard) {
            buf.append(6);
        } else if (view.getId() == R.id.setting_center_key_seven_numberKeyBoard) {
            buf.append(7);
        } else if (view.getId() == R.id.setting_center_key_eight_numberKeyBoard) {
            buf.append(8);
        } else if (view.getId() == R.id.setting_center_key_nine_numberKeyBoard) {
            buf.append(9);
        } else if (view.getId() == R.id.setting_center_key_minus_numberKeyBoard) {
            isMinus = !isMinus;
        } else if (view.getId() == R.id.setting_center_key_zero_numberKeyBoard) {
            buf.append(0);
        } else if (view.getId() == R.id.setting_center_key_dot_numberKeyBoard) {
            buf.append(".");
        } else if (view.getId() == R.id.setting_center_key_delete_numberKeyBoard) {
            if (buf.length() > 0) {
                buf.delete(buf.length() - 1, buf.length());
            }

        } else if (view.getId() == R.id.setting_center_key_clear_numberKeyBoard) {
            buf.delete(0, buf.length());

        } else if (view.getId() == R.id.setting_center_key_sure_numberKeyBoard) {
            if (buf.length() > 0) {
                String psd = buf.toString().trim();


                if (NUM==mType) {

                    //数值回调
                    if (mkeyBoardListener != null) {
                        mkeyBoardListener.onClink(true, psd);
                    }
                    dismiss();



                } else if (PSD==mType) {


                    if (Constants.ADMINI_PASSWORDS.equals(psd) | MySystemUtil.checkPassWords(psd)) {
                        //数值回调
                        if (mkeyBoardListener != null) {
                            mkeyBoardListener.onClink(true, psd);
                        }
                        dismiss();
                    } else {
                        etErrorAlert.setText("密码有误！");
                    }
                }


            } else {
                etErrorAlert.setText("密码不能为空！");
            }
        }
        message.what = CHANGEUI;
        message.obj = buf.toString().trim();
        handler.sendMessage(message);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CHANGEUI:
                    StringBuffer stringBuffer=new StringBuffer();
                    if (buf.length()>0){
                        for (int i = 0; i <buf.length() ; i++) {
                            stringBuffer.append("*");
                        }
                    }
                    etInput.setText(stringBuffer.toString());
                    break;
            }
        }
    };

    public void setKeyBoardType(int type) {
        mType = type;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        Toast.makeText(mContext, "销毁键盘", Toast.LENGTH_SHORT).show();

    }
}
